<!-- favourites_table.php -->

<!-- php script for the table -->

<?php
	//start the session
	session_start();
	
	//checks if user has logged in
	if(!isset($_SESSION['username']))
	{
		$_SESSION['message'] = "<span style='color: red; font-weight: bold;'>You are not logged in to view this page.</span>";
		header('Location: Login.php');
	}
	
	//checks that the form has been submitted
 	if(isset($_POST['submit']))
	{
		$category_sort = $_POST['category_sort'];
		$sort_by = $_POST['sort_by'];
		
		//sets a variable for the file name
		$bookmarks_file = 'bookmarks_data/bookmark_database.txt';
		//open's the file to allow us to read and write to it
		$favourites = file($bookmarks_file);
		
		//create empty arrays for category and name ordering
		$categoryArray=array();
		$nameArray=array();
		foreach ($favourites as $key => $value) 
		{
			$smallArray = explode(" | ", $value);
			$category = trim($smallArray[0]);
			$webAddress = trim($smallArray[1]);
			$pageName = trim($smallArray[2]);
			$date_timestamp = trim($smallArray[3]);
			$visitCounter = trim($smallArray[4]);
			
			//add a new item to categoryArray
			$categoryArray[$key] = $category . " | " . $webAddress . " | " . $pageName . " | " . $date_timestamp . " | " . $visitCounter;
			
			//add new item to nameArray
			$nameArray[$key] = $pageName . " | " . $webAddress . " | " . $category . " | " . $date_timestamp . " | " . $visitCounter;
		}
		
		if($category_sort == 'Category:' || $sort_by == 'Sort by:')
		{
			if($category_sort == 'Category:')
			{
				$_SESSION['message'] = "<span style='color: red; font-weight: bold;'>A Category has not been selected.</span>";
				header('Location: sort_page.php');
			}
			else if($sort_by == 'Sort by:')
			{
				$_SESSION['message'] = "<span style='color: red; font-weight: bold;'>A sorting method has not been selected</span>";
				header('Location: sort_page.php');
			}
		}
		else
		{
			if($category_sort == 'All' || $sort_by == 'Name')
			{
				sort($nameArray);
			}
		}
	}
?>

<!-- html where the table is created and variables added etc -->

<html>
	<head>
		<title>Favourite Table</title>
		<link rel="stylesheet" type="text/css" href="css/style.css" />
	</head>
	
	<body>
		<div id="wrapper">
			<div id="banner"><img src="images/bannerv0.jpg" alt="header graphic"></div>
			
			<div id="wrapper_links"><?php include("menu/menu.php");?></div>
				
			<div id="wrapper_body">

				<p align="left">User: <?php echo $_SESSION['username'];?></p>

				<h3>Favourites List</h3>
		      
				<p>Click <a href="sort_page.php">here</a> to go back to previous page</p> 
   						
				<center>
					<table border="1" bgcolor="#202020">
						<tr>
							<th>Category</th>
							<th>Name</th>
							<th>Date</th>
							<th>Counter</th>
							<th>Delete</th>
						</tr>
						
						<tr>
							<td><?php echo $categoryArray[0]?></td>
							<td><a href="processes/count.php?url=<?php echo $favourite['url'];?>"><?php echo $nameArray[0];?></a></td>
							<td><?php echo date('Y/m/d', $favourite['date']);?></td>
							<td><?php echo $favourite['counter'];?></td>
							<td><a href="processes/delete_favourite.php?url=<?php echo $favourite['url'];?>">Delete</a></td>
						</tr>
						
					</table>
				</center>

				<?php
					if(isset($_SESSION['message']))
					{
						echo $_SESSION['message'] . '<br />';
					}
				?>
					
			</div>
		</div>
	</body>
</html>